<?php
require_once('conn.inc.php');
@$conn		=		mysql_connect($db_host,$db_user,$db_pwd);
mysql_query("set names 'utf8'");
mysql_select_db($db_name);

// @$hid = 6666;
// @$id = 1;			//登陆者id
// @$fid = 2;			//组局人id
@$hid = $_POST['hid'];			//房间id
@$id = $_POST['id'];			//登陆者id
@$fid = $_POST['fid'];			//组局人id
@$posttime=time();

@$select	=	"select * from home where hid='$hid' && state=1"; //LIMIT 0,10
@$sql = "select * from hometeam where zid= '$fid' && state=1";

$team	= mysql_query($sql);
$teamnum	=	mysql_num_rows($team);				//查询到的已参加人数

$rs		= mysql_query($select);
$num_rows = mysql_num_rows($rs);					//查询到的房间个数

$row = mysql_fetch_row($rs);
$ycjrs = $row['nop'];									//查询设置人数

if ($teamnum >= $ycjrs) {
	$rc = array('code'=>-3);
	echo json_encode($rc);										//参加人数到上限
	return ;
}

if($num_rows != 1){
	$rc = array('code'=>-1);
	echo json_encode($rc);										//没有这个房间
	return ;
}

if($id == null && $hid == null){
	$rc = array('code'=>-2);
	echo json_encode($rc);										//用户未登录，id为空或hid为空
	return ;
}


$sql	=	"insert into hometeam (zid,fid,hid,state,posttime) values ('$fid','$id','$hid','1','$posttime')";
$rsql = mysql_query($sql);
$nsql = mysql_affected_rows();
if ($nsql == 1) {
	$rc = array('code'=>1);
	echo json_encode($rc);			//成功
}
mysql_close();
